#include "playpage.h"
#include<QPainter>
#include<QMouseEvent>
#include<QIcon>
#include<QPushButton>
PlayPage::PlayPage(QWidget *parent) : QWidget(parent)
{
    //游戏界面设置
    setFixedSize(700,1000);
    setWindowTitle("飞机大战2.0");
    setWindowIcon(QPixmap(":/tpw/logo.jpeg"));

    //定时器
    timer->start(42);//42ms动一次
    connect(timer,&QTimer::timeout,[=](){
        //每次地图向下移动10
        posY+=10;
        posYY+=10;
        maprecorder++;
        update();//地图更新
        //地图超出游戏界面后重新回到上面
        if(posY>=this->height())
        {
            posY=-1000;
        }
        if(posYY>=this->height())
        {
            posYY=-1000;
        }

    });
}
//游戏界面的绘制
void PlayPage::paintEvent(QPaintEvent *)
{

    QPainter painter(this);
    //两张地图背景
    if(maprecorder<1000)
    {
        painter.drawPixmap(0,posY,700,1000,QPixmap(":/tpw/forest.jpeg"));
        painter.drawPixmap(0,posYY,700,1000,QPixmap(":/tpw/forest.jpeg"));
    }
    if(maprecorder>=1000)
    {
        painter.drawPixmap(0,posY,700,1000,QPixmap(":/tpw/night.jpg"));
        painter.drawPixmap(0,posYY,700,1000,QPixmap(":/tpw/night.jpg"));
    }
    if((maprecorder>=950&&maprecorder<970)||(maprecorder>=990&&maprecorder<1100))
    {
        painter.drawPixmap(280,450,140,50,QPixmap(":/tpw/warning.png"));
    }
    //飞机绘制
    if(p_plane.pfree==false)
    painter.drawPixmap(p_plane.planeX,p_plane.planeY,100,100,p_plane.planee);

}
//鼠标事件
void PlayPage::mouseMoveEvent(QMouseEvent *event)
{
    p_plane.planeX=event->x()-50;
    p_plane.planeY=event->y()-50;//减去50，让指针指在飞机中间
    p_plane.prec.moveTo(p_plane.planeX,p_plane.planeY);//飞机的矩形框变化，用于碰撞检测
    if(p_plane.planeY<=0)
        p_plane.planeY=0;
    if(p_plane.planeY>=920)
        p_plane.planeY=920;
    if(p_plane.planeX<=0)
        p_plane.planeX=0;
    if(p_plane.planeX>=620)
       p_plane.planeX=620;
}


//地图的初始位置
int PlayPage::posY=0;
int PlayPage::posYY=-1000;
